package com.glsc.ngateway.amdoc.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.glsc.ngateway.amdoc.mapper.*;
import com.glsc.ngateway.amdoc.pojo.*;
import com.glsc.ngateway.amdoc.wsclients.WsClient;
import com.glsc.ngateway.amdoc.wsclients.wsdl.*;
import com.glsc.ngateway.common.api.amdoc.dto.AssetProductRequestComprehensiveDto;
import com.glsc.ngateway.common.api.amdoc.dto.AssetProductRequestNodeListViewDto;
import com.glsc.ngateway.common.api.amdoc.dto.AssetProductWorkFlowListViewDto;
import com.glsc.ngateway.common.api.amdoc.dto.InvestmentManagerDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 对于查询而言,service仅仅提供了分页功能
 */


@Service
public class AssetProductServiceImpl implements IAssetProductService {

    Logger logger = LoggerFactory.getLogger(AssetProductServiceImpl.class);

    @Autowired
    public AssetProductViewMapper assetProductViewMapper;

    @Autowired
    public AssetProductMemberViewMapper apmvMapper;

    @Autowired
    public AssetProductWorkFlowViewMapper apwfMapper;

    @Autowired
    public AssetProductDocDirectoryViewMapper apddMapper;

    @Autowired
    public AssetProductDirectoryTemplateViewMapper apdtMapper;

    @Autowired
    public AssetProductDocViewMapper apdMapper;

    @Autowired
    public AssetFlowMapper assetFlowMapper;

    @Autowired
    public HRMResourceMapper hrmResourceMapper;

    @Autowired
    public AssetProductSaleAgencyViewMapper saleAgencyMapper;
    @Autowired
    public AssetProductDefaultMemberViewMapper defaultMemberMapper;

    @Autowired
    WsClient wsClient;

    @Autowired
    public AssetProductWorkFlowListViewMapper assetProductWorkFlowListViewMapper;

    @Autowired
    public AssetProductRequestNodeListViewMapper assetProductRequestNodeListViewMapper;

    @Override
    public PageInfo<AssetProduct> getAssetProductList(Integer pageNum, Integer pageSize, String productCode, String product, String productNature, Integer productSysId) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> assetProductViewMapper.getAssetProductList(productCode, product, productNature, productSysId));
    }

    @Override
    public PageInfo<AssetProductMember> getAssetProductStaffListBySysId(Integer pageNum, Integer pageSize, Integer productSysId) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> apmvMapper.getAssetProductStaffListBySysId(productSysId));
    }

    @Override
    public PageInfo<AssetProductWorkFlow> getAssetProductWorkFlowListBySysId(Integer pageNum, Integer pageSize, Integer productSysId, List<Integer> flowList, String requestName) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> apwfMapper.getAssetProductWorkFlowList(String.valueOf(productSysId), flowList, requestName));
    }

    @Override
    public PageInfo<AssetProductDocDirectory> getAssetProductDocDirectoryListBySysId(Integer pageNum, Integer pageSize, Integer productSysId) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> apddMapper.getAssetProductDocDirectoryList(productSysId));
    }

    @Override
    public PageInfo<AssetProductDirTemp> getAssetProductDirTemplate(Integer pageNum, Integer pageSize, String productNature) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> apdtMapper.getAssetProductDirTemplate(productNature));
    }

    @Override
    public PageInfo<AssetProductDoc> getAssetProductDocListBySysId(Integer pageNum, Integer pageSize, Integer productSysId, Integer directory, String document) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> apdMapper.getAssetProductDocList(productSysId, directory, document));
    }

    @Override
    public PageInfo<AssetProductDocComprehensive> getAssetProductDocListComprehensive(AssetProductRequestComprehensiveDto requestDto) {
        return PageHelper.startPage(requestDto.getPageNo(), requestDto.getPageSize()).doSelectPageInfo(() -> apdMapper.getAssetProductDocListComprehensive(requestDto));
    }

    @Override
    public PageInfo<AssetFlow> getOaWorkflowRequestPage(Integer pageNum, Integer pageSize, List<Integer> flowList, String requestName, List<String> currentNodeIdList) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> assetFlowMapper.getAssetFlowInfo(flowList, requestName, currentNodeIdList));
    }

    @Override
    public AssetFlow getOaWorkflowInfoByRequestId(Integer requestId) {
        if(requestId==null||requestId<1){
            return null;
        }
        return assetFlowMapper.getByRequestId(requestId);
    }

    @Override
    public PageInfo<AssetProductSaleAgency> getAssetProductSaleAgencyList(Integer pageNum, Integer pageSize) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> saleAgencyMapper.getAssetProductSaleAgencyList());
    }

    @Override
    public PageInfo<AssetProductDefaultMember> getAssetProductDefaultMemberList(Integer pageNum, Integer pageSize) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> defaultMemberMapper.getAssetProductDefaultMemberList());
    }

    @Override
    public String createProduct(String productInfo) {
        CreateProduct req = new CreateProduct();
        req.setIn0(productInfo);
        CreateProductResponse res = (CreateProductResponse) wsClient.callWebService(req);
        return res.getOut();
    }

    @Override
    public String updateProduct(String productInfo) {
        UpdateProduct req = new UpdateProduct();
        req.setIn0(productInfo);
        UpdateProductResponse res = (UpdateProductResponse) wsClient.callWebService(req);
        return res.getOut();
    }

    @Override
    public String batupload(String productInfo) {
        Batupload req = new Batupload();
        req.setIn0(productInfo);
        BatuploadResponse res = (BatuploadResponse) wsClient.callWebService(req);
        return res.getOut();
    }

    @Override
    public List<InvestmentManagerDto> getInvestmentManagerInfo(List<Integer> idList) {
        //id=21,22,23
        return hrmResourceMapper.getInvestmentManagerInfo(idList);
    }

    @Override
    public PageInfo<AssetProductWorkFlowListView> getAssetProductWorkFlowListView(Integer pageNum, Integer pageSize, AssetProductWorkFlowListViewDto assetProductWorkFlowListViewDto) {
        return PageHelper.startPage(pageNum, pageSize).setOrderBy("WORKFLOWNAME,WORKFLOWID ASC").doSelectPageInfo(() -> assetProductWorkFlowListViewMapper.selectList(assetProductWorkFlowListViewDto));
    }

    @Override
    public PageInfo getAssetProductRequestNodeListView(Integer pageNum, Integer pageSize, AssetProductRequestNodeListViewDto assetProductRequestNodeListViewDto) {
        return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> assetProductRequestNodeListViewMapper.selectList(assetProductRequestNodeListViewDto));
    }


}
